package my.engine.core;

import my.engine.objects.RequestBean;
import my.engine.objects.UrlMapping;
import my.engine.utils.SettingsUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;
import java.util.logging.Level;

/**
 * Created by IntelliJ IDEA.
 * User: emikolenko
 * Date: 05.03.2008
 * Time: 11:35:45
 */
public class FlowManager {

    public static Map<String, UrlMapping> mappings = new HashMap<String, UrlMapping>();

    public static String defaultUrl;
    public static String defUkUrl;
    public static String defRuUrl;
    public static String defEnUrl;

    public static void parseMappings() {
        try {
            CoreUtils.parseMappings(mappings);
        } catch (Exception e) {
            Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, "mapping is not available", e);
        }
    }

    public static RequestBean getRequestBean(HttpServletRequest request) {
        RequestBean bean = new RequestBean();

        String uri = request.getPathInfo();
        String lang = null;
        String view = null;
        if (uri.startsWith("/public/")) {
            uri.substring(8);
        }

        if (uri.startsWith(Consts.sUK)) {
            lang = Consts.uk;
            view = uri.substring(3);
        } else if (uri.startsWith(Consts.sRU)) {
            lang = Consts.ru;
            view = uri.substring(3);
        } else if (uri.startsWith(Consts.sEN)) {
            lang = Consts.en;
            view = uri.substring(3);
        } else {
            lang = SettingsUtil.getProperty(Consts.defLang);
        }

        if (view != null && view.startsWith("/")) {
            view = view.substring(1);
        }
        if (view != null && view.endsWith("/")) {
            view = view.substring(0, view.length() - 1);
        }

        bean.setLang(lang);
        bean.setView(view);
        bean.setParams(request.getParameterMap());
        return bean;
    }

    public static Map<String, UrlMapping> getMappings() {
        return mappings;
    }

    public static void setMappings(Map<String, UrlMapping> mappings) {
        FlowManager.mappings = mappings;
    }

    public static void prepareLangs(HttpServletRequest request) {
        request.setAttribute(Consts.ukVisible, Boolean.parseBoolean(SettingsUtil.getProperty(Consts.ukVisible)));
        request.setAttribute(Consts.ruVisible, Boolean.parseBoolean(SettingsUtil.getProperty(Consts.ruVisible)));
        request.setAttribute(Consts.enVisible, Boolean.parseBoolean(SettingsUtil.getProperty(Consts.enVisible)));
        request.setAttribute(Consts.defUkUrl, FlowManager.defUkUrl);
        request.setAttribute(Consts.defRuUrl, FlowManager.defRuUrl);
        request.setAttribute(Consts.defEnUrl, FlowManager.defEnUrl);
    }


    public static void prepareLocale(HttpServletRequest request, String lang) {
        HttpSession session = request.getSession();
        Locale loc = null;
        if (lang.equals(Consts.uk)) {
            loc = new Locale("uk", "UA");
        } else if (lang.equals(Consts.ru)) {
            loc = new Locale("ru", "RU");
        } else if (lang.equals(Consts.en)) {
            loc = Locale.ENGLISH;
        }
        
        session.setAttribute(Consts.lang, lang);
        request.setAttribute("lang", lang);
        request.setAttribute("currentClientLocale", loc);
    }
}
